package com.webside.cdn.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.webside.cdn.config.AppConstants;
import com.webside.cdn.service.AuthBiz;


@Controller
public class AuthController {
	protected static String TAG="AuthController";
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private AuthBiz authBiz;
	/*@Resource(name="mongoTemplate")
	private MongoTemplate mongoTemplate;
	@Resource(name="redisTemplate")
	private RedisTemplate<String, String> redisTemplate;*/
	
	/*@RequestMapping(method = RequestMethod.GET, value = "/mongo")
	@ResponseBody
	public String testMongo(){
		DBCollection collection=mongoTemplate.getCollection("proxyIP_collection");
		System.out.println("ProxyIP记录数："+collection.count());
		
		collection.find();
		List<DBObject> list = collection.find().toArray();  
        if(list != null&& list.size() > 0){  
        	BasicDBList dbList= (BasicDBList) list.get(0).get("proxy_ip");
        	System.out.println(dbList.get(0));
        }  

//		Person p=new Person("Joe", 24);
//		mongoTemplate.insert(p);
//		
//		p=mongoTemplate.findById(p.getId(), Person.class);
//		System.out.println(p.id+","+p.name+","+p.age);
		return "ProxyIP记录数："+list.size();
	}*/
	
	/*@RequestMapping(method = RequestMethod.GET, value = "/redis")
	@ResponseBody
	public String testRedis(){
		redisTemplate.opsForHash().put("yumai", "firstName", "mai");
		redisTemplate.opsForHash().put("yumai", "lastName", "yu");
		redisTemplate.opsForSet().add("user", "yumai");
		
		Collection<String> keys=redisTemplate.opsForSet().members("user");
		String firstName=(String)redisTemplate.opsForHash().get("yumai", "firstName");
		String lastName=(String)redisTemplate.opsForHash().get("yumai", "lastName");
		
		return lastName+firstName;
	}*/
	
	@RequestMapping(method = RequestMethod.POST, value = "/auth")
	@ResponseBody
	public String getToken(String accessKey, String decodeType){
		logger.info(TAG+": getToken() start");
		String token=authBiz.auth(accessKey, decodeType);
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (token != null) {
			map.put(AppConstants.TOKEN, token);
			map.put(AppConstants.STATUS_CODE, AppConstants.STATUS_SUCCESS);
			map.put(AppConstants.DATA, null);
			map.put(AppConstants.MSG, AppConstants.STATUS_SUCCESS);
			logger.info(TAG+": auth success");
		} else {
			map.put(AppConstants.TOKEN, token);
			map.put(AppConstants.STATUS_CODE, AppConstants.STATUS_FAIL);
			map.put(AppConstants.DATA, null);
			map.put(AppConstants.MSG, AppConstants.STATUS_FAIL);
			logger.info(TAG+": auth fail");
		}
		logger.info(TAG+": getToken() end  return:"+map);
		return JSON.toJSONString(map);
	}
	
	public static String cdnBasePath="http://222.68.221.60:8080";
	@RequestMapping(method = RequestMethod.GET, value = "/resource")
	public void requestCDN(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String resName=request.getParameter("res");
		String appId=request.getParameter("appId");
		String token=null;
		if(appId!=null&&appId.equals("1001")){
			token=authBiz.generateCDNToken();
		}else{
			token=request.getParameter("token");
		}
		response.sendRedirect(cdnBasePath+"/media/video/"+resName+"?appid=GuoXin-http&token="+token);
	}
	
	/*private String getFullURL(HttpServletRequest request) {
		StringBuffer url = request.getRequestURL();
		if (request.getQueryString() != null) {
			url.append('?');
			url.append(request.getQueryString());
		}
		return url.toString();
	}*/
	
	/*class Person{
		public Person(String name, int age){
			super();
			this.name=name;
			this.age=age;
		}
		private int id;
		private String name;
		private int age;
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
	}*/
}
